<?php

use Plugins\Edge\Admin\Controller\NewsController;
use App\Common\Util\TP;
use Sc\Util\HtmlStructure\Form;
use Sc\Util\HtmlStructure\Form\FormItem;
use Sc\Util\HtmlStructure\Html\Html;
use Plugins\Edge\Common\Enums\NewsEnumIsTop;
use Plugins\Edge\Common\Enums\NewsEnumStatus;

Html::create('添加新闻资讯');

$form = Form::create('newsForm')->config('label-width', '80px');

$form->addFormItems(
    FormItem::hidden('id', 'ID'),
    FormItem::text('title', '标题'),
    FormItem::textarea('intro', '简介'),
    FormItem::upload('cover', '封面')->setUploadUrl(TP::route()->to([\App\Admin\Controller\Base\CommonController::class, 'upload']))->toImage(),
    FormItem::select('tag', '标签')->col(12)->setVAttrs('style','width:100%')->multiple()->setVAttrs('allow-create'),
    FormItem::text('author', '作者')->col(12),
    FormItem::radio('is_top', '置顶')->col(12)->default(NewsEnumIsTop::No->value)->options(NewsEnumIsTop::mapping()),
    FormItem::radio('status', '状态')->col(12)->default(NewsEnumStatus::Listing->value)->options(NewsEnumStatus::mapping()),
    FormItem::editor('content', '内容')->setUploadUrl(TP::route()->to([\App\Admin\Controller\Base\CommonController::class, 'froalaUpload'])),
    FormItem::submit()->successClose('parent')->submitUrl(
        TP::route()->to([NewsController::class, 'store'])->url(),
        TP::route()->to([NewsController::class, 'update'])->url(),
    )->success('parent.VueApp.newsGetData()')
);

if(TP::request()->get('id')){
    $data = Plugins\Edge\Common\Model\News::find(TP::request()->get('id'))?->toArray() ?: [];
    $form->setData($data);
}
return $form->render();